import accessory from '../../store/modules/accessory'

export default {
  data() {
    return {
      keyWords: '', //搜索
      vin: 0,
      selCar: {}, //手动添加的车型
      shape: 'round',
      clearabled: true,
      showAction: true,
      inputAlign: 'left',
      addresslist: [],
      Ssaddresslist: [],
      indexlist: [],
      Xsyc: true,
      storelist: [],
      Deletetext: '删除',
      deletelookann: false,
      carlistxh: [],
      //扫描结果
      show: false,
      contentxq: {},
      showsb: false, //扫描失败
      result: '扫描结果', //扫描搜索结果
      historyList: [],
      scanFlag: false,
      isSwicthCar: false, //是否从切换车型进去
    }
  },
  onLoad(e) {
    if (e.type) {
      this.carlistxh = this.$store.state.accessory.carList
      console.log(this.carlistxh)
      this.isSwicthCar = true
    }
    //品牌，系列，车子
    uni.$on('specifAll', (data) => {
      console.log(data)
      if (data.brandlist) {
        Object.assign(this.selCar, data.brandlist)
      } else if (data.Serieslist) {
        Object.assign(this.selCar, {
          cartypeid: data.Serieslist.carTypeId,
          cartypename: data.Serieslist.carTypeName,
          brand_Logo: '/goodimg/brandimg/' + this.selCar.bid + '.png',
        })
      } else {
        Object.assign(this.selCar, data.Configulist)
        this.carlistxh = []
        this.$set(this.carlistxh, 0, this.selCar)
      }
    })
    this.getHistory()
  },

  onShow() {
    if (this.scanFlag) {
      this.readPic()
    }
  },
  methods: {
    //获取历史记录
    getHistory() {
      const value = uni.getStorageSync(
        'Ztc_' + this.$store.state.tid + '_AccessoryList'
      )
      if (value) {
        this.historyList = value
      }
    },

    //读取图片信息
    readPic() {
      this.scanFlag = false
      const img = uni.getStorageSync('isVal')
      if (!img) {
        return
      }
      uni.showLoading({
        title: 'VIN识别中',
      })
      const errFn = () => {
        uni.hideLoading()
        this.showsb = true
      }
      uni.compressImage({
        src: img,
        quality: 35,
        success: async (res) => {
          let imgUrl, imgInfo
          try {
            imgUrl = await this.$api.files.uploadImg({
              filePath: res.tempFilePath,
            })
          } catch (e) {
            errFn()
          }
          try {
            imgInfo = await this.$api.accessory.getVin({
              imageFilePath: imgUrl.baseUrl + imgUrl.url,
            })
            if (imgInfo.data.status === 1) {
              errFn()
            } else {
              this.vin = imgInfo.data.vin
              uni.hideLoading()
              this.getCarAdaptation(1)
            }
          } catch (e) {
            console.log(e)
            errFn()
          }
        },
      })
    },
    //根据vin搜索车型 type 0 输入搜索 1 图片搜索
    getCarAdaptation(type) {
      if (type === 0 && this.vin.length < 17) {
        uni.showToast({
          title: '请输入正确的vin码',
          icon: 'none',
        })
      }
      this.$api.accessory
        .getCarAdaptation({
          vin: type ? this.vin : this.keyWords,
          returnCount: 20,
        })
        .then((res) => {
          if (res.data.length > 0) {
            this.carlistxh = []
            this.carlistxh = [...res.data]
            this.keyWords = ''
          } else {
            this.showsb = true
          }
        })
        .catch((e) => {
          console.log(e)
        })
    },

    //删除按钮显示和隐藏
    deleteclick(falses) {
      if (falses == '删除') {
        this.Deletetext = '完成'
        this.deletelookann = true
      } else {
        this.Deletetext = '删除'
        this.deletelookann = false
        uni.setStorageSync(
          'Ztc_' + this.$store.state.tid + '_AccessoryList',
          this.historyList
        )
      }
    },

    //删除历史适配车型
    deletexinxi(index) {
      this.historyList.splice(index, 1) //取消
      if (this.historyList.length < 1) {
        this.Deletetext = '删除'
        this.deletelookann = false
        uni.setStorageSync(
          'Ztc_' + this.$store.state.tid + '_AccessoryList',
          this.historyList
        )
      }
    },

    //扫描
    async scanning() {
      this.scanFlag = true
      uni.navigateTo({
        url: '/pages_findAccessory/scan/scan?type=vin',
      })
    },

    //手动添加车型
    manuallyaddmodels() {
      uni.navigateTo({
        url: '/pages_common/chooseSpecifications/chooseSpecifications?type=120',
      })
    },

    //选择车型适配
    adaptation(content) {
      const itemKey = `${content.bid}${content.cartypeid}${content.id}`
      this.historyList.unshift({ key: itemKey, ...content })
      for (let i = 1; i < this.historyList.length; i++) {
        if (this.historyList[i].key === itemKey || i > 4) {
          this.historyList.splice(i, 1)
        }
      }
      uni.setStorageSync(
        'Ztc_' + this.$store.state.tid + '_AccessoryList',
        this.historyList
      )
      this.$store.commit('accessory/setCarInfo', content)
      this.$store.commit('accessory/setCarList', this.carlistxh)
      if (this.isSwicthCar) {
        uni.navigateBack({
          delta: 1,
        })
      } else {
        uni.navigateTo({
          url: '/pages_findAccessory/selectCategory/selectCategory?type=0',
        })
      }
      uni.$off('specifAll')
    },

    quxiaosmioa() {
      this.showsb = false
    },

    chongxsm() {
      this.showsb = false
      this.scanning()
    },
  },
}
